import { saveOssFile, getOssToken, getOssFileInfo, getStudentList, getTeacherList, getCourseList, getClassList, getUserInfo } from '@/services/common';
import { filterObj, BASE_API_VERSION } from '@/utils';
import Require from '@/utils/Require.js';
export default {
  name: 'common',
  namespaced: true,
  state: {
    saveOssFileSource: null,
    ossTokenSource: null,
    ossFileInfoSource: null,
    studentListSource: null,
    teacherListSource: null,
    courseListSource: null,
    classListSource: null,
    userInfoSource: null
  },
  mutations: {
    setSaveOssFile(state, val) {
      state.saveOssFileSource = val;
    },
    setOssFileInfo(state, val) {
      state.ossFileInfoSource = val;
    },
    setOssToken(state, val) {
      state.ossTokenSource = val;
    },
    setStudentList(state, val) {
      state.studentListSource = val;
    },
    setTeacherList(state, val) {
      state.teacherListSource = val;
    },
    setCourseList(state, val) {
      state.courseListSource = val;
    },
    setClassList(state, val) {
      state.classListSource = val;
    },
    setUserInfo(state, val) {
      state.userInfoSource = val;
    }
  },
  actions: {
    async treeAndAllUser() {
      const r = await Require({
        api: {
          url: process.env.NODE_ENV === 'development' ? `/common/base-platform/${BASE_API_VERSION}/base/grade/treeAndAllUser` : `/base-platform/${BASE_API_VERSION}/base/grade/treeAndAllUser`
        }
      });
      if (r.success) {
        return {
          code: 0,
          data: r.data,
          message: r.message
        };
      } else {
        this.$message.error(r.message);
      }
    },
    async allTeacherAndTree() {
      const r = await Require({
        api: {
          url: process.env.NODE_ENV === 'development' ? `/common/base-platform/${BASE_API_VERSION}/base/department/allTeacherAndTree  ` : `/base-platform/${BASE_API_VERSION}/base/department/allTeacherAndTree  `
        }
      });
      if (r.success) {
        return {
          code: 0,
          data: r.data,
          message: r.message
        };
      } else {
        this.$message.error(r.message);
      }
    },
    async saveOssFile({ commit }, params) {
      await saveOssFile(filterObj(params)).then(res => {
        commit('setSaveOssFile', res.data || null)
      }).catch(() => {
        commit('setSaveOssFile', null)
      })
    },
    async getOssFileInfo({ commit }, params) {
      await getOssFileInfo(filterObj(params)).then(res => {
        commit('setOssFileInfo', res.data || null)
      }).catch(() => {
        commit('setOssFileInfo', null)
      })
    },
    async getOssToken({ commit }, params) {
      await getOssToken(filterObj(params)).then(res => {
        commit('setOssToken', res.data || null)
      }).catch(() => {
        commit('setOssToken', null)
      })
    },
    async getStudentList({ commit }, params) {
      await getStudentList(filterObj(params)).then(res => {
        commit('setStudentList', res.data || null)
      }).catch(() => {
        commit('setStudentList', null)
      })
    },
    async getTeacherList({ commit }, params) {
      await getTeacherList(filterObj(params)).then(res => {
        commit('setTeacherList', res.data || null)
      }).catch(() => {
        commit('setTeacherList', null)
      })
    },
    async getCourseList({ commit }, params) {
      await getCourseList(params).then(res => {
        commit('setCourseList', res.data || null)
      }).catch(() => {
        commit('setCourseList', null)
      })
    },
    async getClassList({ commit }, params) {
      await getClassList(params).then(res => {
        commit('setClassList', res.data || null)
      }).catch(() => {
        commit('setClassList', null)
      })
    },
    async getUserInfo({ commit }, params) {
      await getUserInfo(params).then(res => {
        commit('setUserInfo', res.data || null)
      }).catch(() => {
        commit('setUserInfo', null)
      })
    }
  }
}